﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Domain;
using Persistence;
using Persistence.Dao;
using Common;
namespace web.manage
{
    public partial class order_operate : adminBase
    {
        int id = DNTRequest.GetQueryInt("id", 0);
        protected Order entity_Order = new Order();

        protected IList<OrderDetailMen> menlist = new List<OrderDetailMen>();
        protected IList<OrderDetailKou> koulist = new List<OrderDetailKou>();
        protected IList<OrderDetailOther> otherlist = new List<OrderDetailOther>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (role > 1)
            {
                btnSet.Enabled = false;
            }
            if (role > 0)
            {
                ddlcaiwu.Enabled = txtDeadlineTime.Disabled = ddlStatus.Enabled = false;
            }

            if (!IsPostBack)
            {
                if (id > 0)
                {
                    entity_Order = Singleton<OrderDao>.Instance.GetModel(id);
                    if (entity_Order != null)
                    {
                        ddlcaiwu.SelectedValue = entity_Order.financeStatus;
                        ddljishu.SelectedValue = entity_Order.technicStatus;
                        ddlStatus.SelectedValue = entity_Order.deleteFlag.ToString();
                        if (entity_Order.deadlineTime != DateTime.MinValue)
                        {
                            txtDeadlineTime.Value = entity_Order.deadlineTime.ToString("yyyy-MM-dd");
                        }
                        tbReply.Text = entity_Order.replyContent;
                    }
                    BindList();
                }
            }
        }
        private void BindList()
        {
            menlist = Singleton<OrderDetailMenDao>.Instance.GetModels("*", "orderid = " + id);
            koulist = Singleton<OrderDetailKouDao>.Instance.GetModels("*", "orderid = " + id);
            otherlist = Singleton<OrderDetailOtherDao>.Instance.GetModels("*", "orderid = " + id);
            if (menlist.Count > 0)
            {
                rptList.Visible = true;
                rptList.DataSource = menlist;
                rptList.DataBind();
            }
            else
            {
                rptList.Visible = false;
            }
            if (koulist.Count > 0)
            {
                Repeater1.Visible = true;
                Repeater1.DataSource = koulist;
                Repeater1.DataBind();
            }
            else
            {
                Repeater1.Visible = false;
            }
            if (otherlist.Count > 0)
            {
                Repeater2.Visible = true;
                Repeater2.DataSource = otherlist;
                Repeater2.DataBind();
            }
            else
            {
                Repeater2.Visible = false;
            }
        }

        protected void btnSet_Click(object sender, EventArgs e)
        {
            System.Collections.Hashtable ht = new System.Collections.Hashtable();
            ht.Add("deleteFlag", Utils.ObjectToInt(ddlStatus.SelectedValue, 0));
            ht.Add("orderId", id);
            ht.Add("financeStatus", ddlcaiwu.SelectedValue);
            ht.Add("technicStatus", ddljishu.SelectedValue);
            DateTime tmp = Utils.ObjectToDateTime(txtDeadlineTime.Value.Trim(), DateTime.MinValue);
            if (tmp != DateTime.MinValue)
            {
                ht.Add("deadlineTime", tmp);
            }
            if (Singleton<OrderDao>.Instance.SetStatusInDetail(ht))
            {
                Response.Write("<script>window.opener.location.href=window.opener.location.href;</script>");
                Message.alertAndClose("审核成功");
            }
            else
            {
                Message.alert("审核失败");
            }
        }

        protected void btnReply_Click(object sender, EventArgs e)
        {
            Order o = new Order();
            o.orderId = id;
            o.replyContent = tbReply.Text.Trim();
            o.replyTime = DateTime.Now;
            if (Singleton<OrderDao>.Instance.Reply(o))
            {
                Message.AlertFresh("回复成功");
            }
        }

        protected void btnSetJindu_Click(object sender, EventArgs e)
        {
            Order o = new Order();
            o.orderId = id;
            o.schedule = tbJinDu.Text.Trim();
            if (Singleton<OrderDao>.Instance.SetJinDu(o))
            {
                Message.AlertFresh("设置订单进度成功");
            }
        }
    }
}
